// pages/my/my.js
var app = getApp()
Page({
  data: {
    agentId:'',
    accountId:'',
    putInCart:false,
    goodsAttr:[],
    currentItem:'',
    currentIndex:'',
    currentBox:[],
    isStock:false,
    enterCart:true,
    //商品信息
    name:'',imgUrl:'',price:'',sales:'',stock:'',productId:'',goodsType:'',thumbUrl:'',stockPrice:'',
    //厂家信息
    brand_url:'',brand_desc:'',brand_name:'',brand:true,
    //配送信息
    logistics:'',
    //商品详情
    details:[],
    num:1,
    actPrice:'',
    totalPrice:'',
    versionValueIds:'',
    manufacturerId:''
  },
  onLoad: function (options) {
    let goodsType = options.goodsType,
        goodsId = options.id,
        goodsPrice = options.price
    this.setData({
      price:goodsPrice,
      productId:goodsId,
      goodsType
    })
    this.getGoodsDetail(goodsId,goodsType)
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      this.creatCurrentBox(agentId,goodsType,goodsId)
      this.setData({
        agentId,
        accountId
      })
      
    })
    if (options.type==1) {
      this.setData({
        isStock:true
      })
    }
    // this.getPrice(this.data.agentId,this.data.goodsType,this.data.productId)
  },
  bindBuy(){
    this.setData({
      putInCart:true,
      enterCart:false
    })
  },
  //加入购物车
  bindEnterCart(){
    this.setData({
      putInCart:true,
      enterCart:true
    })
  },
  bindClose(){
    this.setData({
      putInCart:false
    })
  },
  //选择属性
  bindChoseAttr(e){
    let id = e.currentTarget.dataset.id
    let index = e.currentTarget.dataset.index
    let attrName = e.currentTarget.dataset.attrName
    let currentBox = this.data.currentBox
    //遍历goodsAttr寻找和当前商品类型名相同的对象
    this.data.goodsAttr.forEach(item=>{
      if (item.name==attrName) {
        //遍历该对象下的attr对象获取和当前商品属性名相同的对象
        item.attr.forEach(item=>{
          if (item.name==id) {
            let something = item
            //遍历currentBox替换商品类型index相同的值
            currentBox.forEach((item,i)=>{
              if (i==index) {
                item.item=something
              }
            })
          }
        })
      }
    })
    this.setData({
      currentBox
    })
    this.getPrice(this.data.agentId,this.data.goodsType,this.data.productId)
  },
  //根据商品规格获取库存信息，价格，库存量
  getPrice(agentId,type,productId){
    var that = this
    let data = {
      agentId,
      type,
      productId
    }
    let typeList=''
    this.data.currentBox.forEach(item=>{
      typeList+=item.item.id+','
    })
    data.versionValueIds = typeList.substr(0, typeList.length - 1)
    console.log(data)
    this.setData({
      versionValueIds:typeList.substr(0, typeList.length - 1)
    })
    app.request('wx_app/mall/product/version/stockInfo',data,'GET').then(res=>{
      console.log(res)
      that.setData({
        stock:res.data.stock,
        actPrice:res.data.price,
        thumbUrl:res.data.thumbUrl,
        totalPrice:res.data.price*that.data.num
      })
    })
  },
  //初始化商品规格
  creatCurrentBox(agentId,type,productId){
    var that = this
    var data={
      agentId,
      type,
      productId
    }
    app.request('wx_app/mall/product/version/detail',data,'GET').then(res=>{
      console.log(res)
      let goodsAttr = []
      let attr = []
      //获取商品id的属性名放入content中
      res.data.versions.forEach((item,i)=>{
        let typesId = []
        goodsAttr.push({
          name:item.name,
          attr:item.content
        })
      })
      that.setData({
        goodsAttr
      })
      //获取默认属性放入currentBox
      let currentBox = []
      that.data.goodsAttr.forEach((item,i)=>{
        console.log(item.attr)
        let id = i;
        if (true) {}
        item.attr.forEach((item,index)=>{
          if (index==0) {
            currentBox.push({id,item})
          }
        })
      })
      that.setData({
        currentBox:currentBox
      })
      let typeList=''
      that.data.currentBox.forEach(item=>{
        typeList+=item.item.id+','
      })
      that.setData({
        versionValueIds:typeList.substr(0, typeList.length - 1)
      })
      that.getPrice(this.data.agentId,this.data.goodsType,this.data.productId)
    })
  },
  //将物品加入购物车
  bindPutInCart(){
    if (this.data.actPrice) {    
      this.setData({
        putInCart:false
      })
      let data = {
        agentId:this.data.agentId,
        productType:this.data.goodsType,
        productId:this.data.productId,
        productName:this.data.name,
        price:this.data.actPrice,
        thumbUrl:this.data.thumbUrl,
        // retailPrice:
        count:this.data.num,
        versionValueIds:this.data.versionValueIds,
        manufacturerId:this.data.manufacturerId
      }
      console.log(data)
      app.request('wx_app/stock_cart/saveStockCard',data,'GET').then(res=>{
        console.log(res)
      })
    }else{
      console.log('未加载完成')
    }
  },
  //结算
  bindToCart(){
    if (this.data.actPrice) {    
      let data = {
        agentId:this.data.agentId,
        productType:this.data.goodsType,
        productId:this.data.productId,
        productName:this.data.name,
        price:this.data.actPrice,
        thumbUrl:this.data.thumbUrl,
        // retailPrice:
        count:this.data.num,
        versionValueIds:this.data.versionValueIds,
        manufacturerId:this.data.manufacturerId
      }
      console.log(data)
      app.request('wx_app/stock_cart/saveStockCard',data,'GET').then(res=>{
        console.log(res)
        wx.navigateTo({
          url:'purchaseSettlement'
        })
      })
    }else{
      console.log('未加载完成')
    }
    
  },
  //获得商品详情
  getGoodsDetail(productId,type){
    wx.showLoading({
      title:'正在加载'
    })
    var data = {
      productId,
      type
    }
    console.log(data)
    var that = this
    app.request('wx_app/product/detail',data,'GET').then(res=>{
      console.log(res)
      let details = []
      res.data.details.forEach(item=>{
        details.push(item.url)
      })
      that.setData({
        name:res.data.name,
        imgUrl:res.data.picUrl,
        // sales:res.data.sales,
        price:res.data.price,
        brand_url:res.data.brand_url?res.data.brand_url:'',
        brand_desc:res.data.brand_desc?res.data.brand_desc:'',
        brand_name:res.data.brand_name?res.data.brand_name:'',
        manufacturerId:res.data.manufacturerId,
        // logistics:res.data.logistics,
        details
      })
      if (!res.data.brand_name) {
        that.setData({
          brand:false
        })
      }else{
        that.setData({
          brand:true
        })
      }
      wx.hideLoading()
    })
  },
  add(e){
    let num = e.currentTarget.dataset.num
    num++
    let price = this.data.actPrice
    this.setData({
      num,
      totalPrice:this.data.actPrice?this.data.actPrice*num:this.data.price*num
    })
  },
  sub(e){
    let num = e.currentTarget.dataset.num
    num--
    let price = this.data.actPrice

    if (num>0) {
      this.setData({
        num,
        totalPrice:price*num
      })
    }
  },
})